import Vue from "vue";
import VueRouter from "vue-router";
// 主页
import HomeView from "../views/Home/HomeView.vue";
// 发布页
import MyPublish from "../components/MyPublish.vue";
// 个人页
import MyGeren from "../views/MyGeren.vue";
// 搜索页
import MySearch from "../views/search/MySearch.vue";
// 个人信息
import MyXinxi from "../components/MyXinxi.vue";
// 登录页
import MyLogin from "../views/MyLogin.vue";
// 注册页
import MyEnroll from "../views/MyEnroll";
// 详情页
import MyDetail from "../views/Home/MyDetail";
// 修改密码
import MyRevise from "../views/MyRevise";
// 忘记密码
import MyForget from "../views/MyForget";
// 关于我们
import MyAbout from "../views/MyAbout";
// 联系我们
import MyContact from "../views/MyContact";

// 搜索列表页
import SearchList from "../views/search/SearchList";
// 我的发布页
import MySend from "../views/publish/MySend";
// 浏览历史
import MyHistory from "../views/History/MyHistory";
// 封面
import MyCover from "../views/Cover/MyCover";

Vue.use(VueRouter);

const routes = [
  {
    path: "/cover",
    // redirect:'/',
    component: MyCover,
  },
  {
    path: "/",
    //redirect: "/cover",  //重定向
    name: "home",
    component: HomeView,
    meta: {
      isRoot: true,
      keep: true,
    },
  },
  {
    path: "/publish",
    name: "publish",
    component: MyPublish,
    meta: {
      isRoot: true,
      keep: true,
    },
  },
  {
    path: "/geren",
    // redirect: "/cover",  //重定向
    name: "geren",
    component: MyGeren,
    meta: {
      isRoot: true,
      keep: true,
    },
  },
  {
    path: "/search",
    name: "search",
    component: MySearch,
    meta: {
      todo: true,
    },
  },
  {
    path: "/xinxi",
    name: "xinxi",
    component: MyXinxi,
    meta: {
      todo: true,
    },
  },
  {
    path: "/login",
    name: "login",
    component: MyLogin,
  },
  {
    path: "/enroll",
    name: "enroll",
    component: MyEnroll,
  },
  {
    path: "/detail",
    name: "detail",
    component: MyDetail,
  },
  {
    path: "/revise",
    name: "revise",
    component: MyRevise,
    meta: {
      todo: true,
    },
  },
  {
    path: "/forget",
    name: "forget",
    component: MyForget,
  },
  {
    path: "/about",
    name: "about",
    component: MyAbout,
  },
  {
    path: "/contact",
    name: "contact",
    component: MyContact,
  },
  {
    path: "/searchlist",
    name: "searchlist",
    component: SearchList,
    meta: {
      todo: true,
    },
  },
  {
    path: "/send",
    name: "send",
    component: MySend,
    meta: {
      todo: true,
    },
  },
  {
    path: "/history",
    name: "history",
    component: MyHistory,
    meta: {
      todo: true,
    },
  },
];
import store from "@/store";
import { Toast } from "vant";

const router = new VueRouter({
  routes,
});

// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  let uid = store.state.uid;
  //调取vuex中的 uid

  if (to.meta.todo) {
    if (uid) {
      next();
    } else {
      Toast("请您先登录~");
      setTimeout(() => {
        next("login");
      }, 1500);
    }
  } else {
    next();
  }
});

export default router;
